package com.zjxf.service.impl;

import com.zjxf.base.bean.po.UmsStoreSeller;
import com.zjxf.base.bean.vo.VoUmsStoreSeller;
import com.zjxf.base.common.OperationException;
import com.zjxf.base.common.SysConst;
import com.zjxf.repository.UmsSellerRepository;
import com.zjxf.service.UmsStoreSellerService;
import com.zjxf.utils.*;
import lombok.AllArgsConstructor;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

/**
 * 商家端用户实现
 *
 * @author ZhangWei
 * @date 2021/3/26/026 15:42
 */
@Service
@AllArgsConstructor
@Transactional(rollbackOn = RuntimeException.class)
public class UmsStoreStoreSellerServiceImpl implements UmsStoreSellerService {

    private final UmsSellerRepository umsSellerRepository;

    @Override
    public Optional<UmsStoreSeller> findById(Long id) {
        return umsSellerRepository.findByIdAndDeleteState(id, SysConst.DeleteState.UN_DELETE_STATE.getCode());
    }

    @Override
    public List<UmsStoreSeller> findBySellerIdIn(List<Long> productIdList) {
        Specification<UmsStoreSeller> specification = DynamicSpecifications.bySearchFilterByBasic(Collections.singleton(
                new SearchFilter("id", SearchFilter.Operator.IN, productIdList)));
        return umsSellerRepository.findAll(specification);
    }

    @Override
    public Optional<UmsStoreSeller> findByUsername(String userName) {
        return umsSellerRepository.findByUsernameAndDeleteState(userName, SysConst.DeleteState.UN_DELETE_STATE.getCode());
    }

    @Override
    public RadomUP.UP defaultSeller(Long id) {
        RadomUP.UP up = RadomUP.rUP();
        Optional<UmsStoreSeller> optionalSeller = this.findByUsername(up.getUsername());
        if (optionalSeller.isPresent()) {
            defaultSeller(id);
        } else {
            UmsStoreSeller umsStoreSeller = new UmsStoreSeller();
            umsStoreSeller.setUsername(up.getUsername());
            String salt = Encodes.encodeHex(Digests.generateSalt(Digests.SALT_SIZE));
            umsStoreSeller.setSalt(salt);
            String encryptPassword = UserUtils.getEncryptPassword(up.getPassword(), salt);
            umsStoreSeller.setPassword(encryptPassword);
            umsStoreSeller.setStoreId(id);
            umsSellerRepository.save(umsStoreSeller);
            return up;
        }
        return null;
    }

    @Override
    public void addOrUpdate(VoUmsStoreSeller.addOrUpdate voSeller) throws OperationException {
        Optional<UmsStoreSeller> umsSellerOptionalByUserName = umsSellerRepository.findByUsernameAndDeleteState(voSeller.getUsername(), SysConst.DeleteState.UN_DELETE_STATE.getCode());
        Optional<UmsStoreSeller> umsSellerOptionalById = umsSellerRepository.findByIdAndDeleteState(voSeller.getId(), SysConst.DeleteState.UN_DELETE_STATE.getCode());
        if (umsSellerOptionalByUserName.isPresent()) {
            throw new OperationException("该用户名已存在，请重新输入");
        } else {
            if (umsSellerOptionalById.isPresent()) {
                UmsStoreSeller umsStoreSeller = umsSellerOptionalById.get();
                String salt = Encodes.encodeHex(Digests.generateSalt(Digests.SALT_SIZE));
                umsStoreSeller.setSalt(salt);
                String encryptPassword = UserUtils.getEncryptPassword(voSeller.getPassword(), salt);
                umsStoreSeller.setPassword(encryptPassword);
                umsSellerRepository.save(umsStoreSeller);
            } else {
                UmsStoreSeller umsStoreSeller = new UmsStoreSeller();
                umsStoreSeller.setUsername(voSeller.getUsername());
                String salt = Encodes.encodeHex(Digests.generateSalt(Digests.SALT_SIZE));
                umsStoreSeller.setSalt(salt);
                String encryptPassword = UserUtils.getEncryptPassword(voSeller.getPassword(), salt);
                umsStoreSeller.setPassword(encryptPassword);
                umsStoreSeller.setStoreId(voSeller.getStoreId());
                umsSellerRepository.save(umsStoreSeller);
            }
        }
    }

    @Override
    public List<UmsStoreSeller> findAll() {
        Specification<UmsStoreSeller> specification = DynamicSpecifications.bySearchFilterByBasic();
        return umsSellerRepository.findAll(specification);
    }
}
